<?php
/**
 * Created by PhpStorm.
 * User: FDG296
 * Date: 2017/8/2
 * Time: 16:42
 */
namespace backend\models;

use Yii;
use yii\db\ActiveRecord;

class GoodsSource extends ActiveRecord{
    const STATUS_ENABLE = 1;
    const STATUS_DISABLE = 0;

    public static function tableName(){
        return '{{%goods_source}}';
    }

    public function rules(){
        return [
            ['use_flg','default','value'=>self::STATUS_DISABLE],
            ['creator','getCreator'],
            ['create_time','date']
        ];
    }

    /**
     * @获取创建人
     */
    public function getCreator(){
        $userInfo = \common\helps\tools::getloginUserInfo();
        if($userInfo){
            $userName = $userInfo->userName;
        }else{
            $userName = '';
        }
        return $userName;
    }

    /**
     * @param $data_url array 素材链接
     * @param $data_sort array 素材排序
     * @param $data_type array 素菜类型
     * @param $goods array 商品详情
     */
    public function addSource($data_url,$data_sort,$data_type,$goods){
        $list = array();
        foreach($data_url as $key=>$value){
            $list[] = [$goods['main_goods_id'],$goods['goods_id'],$goods['sku'],
                $data_type[$key],$value,$value,$data_sort[$key],$this->getCreator(),date('Y-m-d H:i:s',time())];
        }
        $field_name = ['main_goods_id','goods_id','sku','source_type','source_url','source_link','sort','creator','create_time'];
        $ret = Yii::$app->db->createCommand()->batchInsert('j_goods_source', $field_name, $list)->execute();
        return $ret;
    }
}